import Vue from "vue";
import Router from "vue-router";
import md5 from "js-md5";

// //import Home from '@/view/Home'
// import TodoTaskList from "@/view/bpm/todoTaskList"

// import ApproveList from "@/view/bpm/approveList"
// import DefinitionList from "@/view/bpm/definitionList"
// import ApplyList from "@/view/bpm/applyList"
// import DraftList from "@/view/bpm/draftList"

// import formCustSqlView from "@/view/custsql/formCustSqlView"

// import ComplateTask from "@/view/bpm/taskComplate"
// import InstanceDetail from "@/view/bpm/instanceDetail"
// import InstanceDetailCopy from "@/view/bpm/instanceDetailCopy"

// import Start from "@/view/bpm/start"
// import Preview from "@/view/bpm/preview"

// import Authorization from "@/view/auth/authorization"
// import dingAuthorization from "@/view/auth/dingAuthorization"
// import wxQyAuthorization from "@/view/auth/wxQyAuthorization"

// // 任务管理
// import TaskCharge from "@/view/taskManagement/taskCharge"
// import TaskHandle from "@/view/taskManagement/taskHandle"
// import TaskPool from "@/view/taskManagement/taskPool"
// import TaskForm from "@/view/taskManagement/taskForm"
// import TaskDetail from "@/view/taskManagement/taskDetail"

// import MessageNotify from "@/view/bpm/messageList"
// import mustLoan from "@/view/bpm/mustLoan"
// import agriculturalLoan from "@/view/bpm/agriculturalLoan"
// import loanEntryInfo from "@/view/bpm/loanEntryInfo"

// import carboncopyReceiveList from "@/view/bpm/carboncopyReceiveList"
// import proRouteSetList from "@/view/bpm/proRouteSetList.vue"

// import Test from "@/view/test"

import setting from "@/setting"

import {
  AlertModule
} from "vux"; // vux对话框
import JSbridge from "@/service/common/GZBJSBridge"

// import router from '../../../bpm-biz-ui-master/src/router'
Vue.use(Router);

const appRouter = new Router({
  mode: setting.routerMode,
  base: setting.routerBase,
  routes: [{
    
    path: "/",
    // name: "Login",
    // redirect: "/loanPage/bankEntrance",
    // redirect: "/loanPage/productHome",
    // path: "/loanPage/productHome",

    // component: resolve => require(["@/view/index"], resolve),
    component: resolve => require(["@/view/loanPage/productHome"], resolve),
    // component: resolve => require(["@/view/productHome"], resolve),
    // component: resolve => require(["@/view/login/login"], resolve),
    // component: resolve => require(["@/view/HelloWorld"], resolve),
    
    // children: [
    //   {
    //   path: "home",
    //   name: "智能微贷",
    //   component: resolve => require(["@/view/Home"], resolve)
    // },
    //   {
    //     path: "news",
    //     name: "News",
    //     component: resolve => require(["@/view/news/newsList"], resolve)
    //   },
    //   {
    //     path: "notice",
    //     name: "Notice",
    //     component: resolve => require(["@/view/notice/noticeList"], resolve)
    //   }
    // ]
  },

  // {
  //   path: '/productHome',
  //   name: '测试',
  //   component: () => import("@/view/HelloWorld.vue")
  // },
  {
    path: '/productIndex',
    name: '企业信息',
    component: () => import("@/view/loanPage/productIndex.vue")
  },
  {
    path: '/productTurnover',
    name: '产品介绍',
    component: () => import("@/view/loanPage/productTurnover.vue")
  },
  //助企流水贷 助企周转贷 办贷页面
 
  {
    path: '/productHome',
    name: '产品介绍',
    component: () => import("@/view/loanPage/productHome.vue")
  },
    // {
    //   path: "*",
    //   name: "404",
    //   component: resolve => require(["@/view/404"], resolve)
    // },
    // {
    //   path: '/dialog',
    //   name: 'dialog',
    //   component: resolve => require(["@/view/dialog"], resolve)
    // },
    // {
    //   path: "/login",
    //   name: "Login",
    //   component: resolve => require(["@/view/login/login"], resolve)
    // },
    // {
    //   path: "/newsDetail",
    //   name: "NewsDetail",
    //   component: resolve => require(["@/view/news/newsDetail"], resolve)
    // },
    // {
    //   path: "/noticeDetail",
    //   name: "NoticeDetail",
    //   component: resolve => require(["@/view/notice/noticeDetail"], resolve)
    // },
    // {
    //   path: "/componentsDemo",
    //   name: "componentsDemo",
    //   component: resolve => require(["@/view/componentsDemo.vue"], resolve)
    // },
    // {
    //   path: "/homeEdit",
    //   name: "HomeEdit",
    //   component: resolve => require(["@/view/editInfo/homeEdit"], resolve)
    // },
    // {
    //   path: "/customize/gzTypeInDetails",
    //   name: "gzTypeInDetails",
    //   component: resolve =>
    //     require(["@/components/customize/gz-type-in-details"], resolve)
    // },
    // {
    //   path: "/auth/recognize",
    //   name: "recognize",
    //   component: resolve => require(["@/view/auth/recognize"], resolve)
    // },
    // {
    //   path: "/auth/authData",
    //   name: "authData",
    //   component: resolve => require(["@/view/auth/authData"], resolve)
    // },
    // {
    //   path: "/auth/authMate",
    //   name: "authMate",
    //   component: resolve => require(["@/view/auth/authMate"], resolve)
    // },
    // {
    //   path: "/auth/mateMsg",
    //   name: "mateMsg",
    //   component: resolve => require(["@/view/auth/mateMsg"], resolve)
    // },
    // {
    //   path: "/auth/verifyFace",
    //   name: "verifyFace",
    //   component: resolve => require(["@/view/auth/verifyFace"], resolve)
    // },
    // {
    //   path: "/customize/gzTypeInData",
    //   name: "gzTypeInData",
    //   component: resolve =>
    //     require(["@/components/customize/gz-type-in-data"], resolve)
    // },
    // {
    //   path: "/customize/gzFaceVerification",
    //   name: "gzFaceVerification",
    //   component: resolve =>
    //     require(["@/components/customize/gz-face-verification"], resolve)
    // },
    // {
    //   path: "/customize/gzPunchList",
    //   name: "打卡",
    //   component: resolve =>
    //     require(["@/components/customize/gz-punch-list"], resolve)
    // },
    // {
    //   path: "/bpm/todoTaskList",
    //   name: "我的待办",
    //   component: TodoTaskList
    // },
    // {
    //   path: "/test",
    //   name: "测试组件页面",
    //   component: Test
    // },
    // {
    //   path: "/bpm/approveList",
    //   name: "办理历史",
    //   component: ApproveList
    // },
    // {
    //   path: "/bpm/definitionList",
    //   name: "发起申请",
    //   component: DefinitionList
    // },
    // {
    //   path: "/bpm/applyList",
    //   name: "申请历史",
    //   component: ApplyList
    // },
    // {
    //   path: "/bpm/draftList",
    //   name: "我的草稿",
    //   component: DraftList
    // },
    //  {
    //   path: "/bpm/definitionList_copy",
    //   name: "发起申请",
    //   component: resolve => require(["@/view/bpm/definitionList_copy"], resolve)
    // },
    // {
    //   path: "/bpm/start",
    //   name: "流程启动",
    //   component: Start
    // },
    // {
    //   path: "/bpm/complateTask",
    //   name: "处理任务",
    //   component: ComplateTask
    // },
    // {
    //   path: "/bpm/instanceDetail",
    //   name: "流程详情",
    //   component: InstanceDetail
    // },
    // {
    //   path: "/bpm/instanceDetailCopy",
    //   name: "流程详情",
    //   component: InstanceDetailCopy
    // },
    // {
    //   path: "/bpm/preview",
    //   name: "表单预览",
    //   component: Preview
    // },
    // {
    //   path: "/authorization",
    //   name: "鉴权",
    //   component: Authorization
    // },
    // {
    //   path: "/abDing",
    //   name: "钉钉鉴权",
    //   component: dingAuthorization
    // },
    // {
    //   path: "/wxQyAuthorization",
    //   name: "钉钉鉴权",
    //   component: wxQyAuthorization
    // },
    // {
    //   path: "/bpm/carboncopyReceiveList",
    //   name: "抄送",
    //   component: carboncopyReceiveList
    // },
    // {
    //   path: "/form/formCustSqlView",
    //   name: "自定义列表",
    //   component: formCustSqlView
    // },
    // {
    //   path: "/task/taskCharge",
    //   name: "任务签收",
    //   component: TaskCharge
    // },
    // {
    //   path: "/task/taskHandle",
    //   name: "任务办理",
    //   component: TaskHandle
    // },
    // {
    //   path: "/task/taskPool",
    //   name: "任务池",
    //   component: TaskPool
    // },
    // {
    //   path: "/task/taskForm",
    //   name: "办理任务",
    //   component: TaskForm
    // },
    // {
    //   path: "/task/taskDetail",
    //   name: "任务详情",
    //   component: TaskDetail
    // },
    // {
    //   path: "/bpm/messageList",
    //   name: "消息通知",
    //   component: MessageNotify
    // },
    // {
    //   path: "/bpm/mustLoan",
    //   name: "我要贷款",
    //   component: mustLoan
    // },
    // {
    //   path: "/bpm/agriculturalLoan",
    //   name: "兴农贷",
    //   component: agriculturalLoan
    // },
    // {
    //   path: "/bpm/loanEntryInfo",
    //   name: "个人信息",
    //   component: loanEntryInfo
    // },
    // {
    //   path: "/bpm/riskRule",
    //   name: "风控规则/征信报告",
    //   component: () => import("@/view/bpm/riskRule.vue")
    // },
    // {
    //   path: "/bpm/proRouteSetList",
    //   name: "贷款申请",
    //   component: proRouteSetList
    // },
    // {
    //   path: "/apply/gz-adopt",
    //   name: "进件结果",
    //   component: () => import("@/view/apply/gz-adopt.vue")
    // },
    // //续贷部分
    // {
    //   path: "/refinance/apply",
    //   name: "申请续贷",
    //   component: () => import("@/view/refinance/apply.vue")
    // },
    // {
    //   path: "/refinance/submit",
    //   name: "提交成功",
    //   component: () => import("@/view/refinance/submit.vue")
    // },
    // {
    //   path: "/refinance/search",
    //   name: "进度查询",
    //   component: () => import("@/view/refinance/search.vue")
    // },
    // //  续贷确认
    // {
    //   path: '/refinance/renewalComfirmation',
    //   name: '续贷确认',
    //   component: () => import("@/view/refinance/renewalConfirmation.vue")
    // },
    // //还钱
    // {
    //   path: '/refinance/payItBack',
    //   name: '还钱',
    //   component: () => import("@/view/refinance/payItBack.vue")
    // },
    // //还款
    // {
    //   path: '/refinance/repaymentConfirmation',
    //   name: '还款',
    //   component: () => import("@/view/refinance/repaymentConfirmation.vue")
    // },
    // //百福支付
    // {
    //   path: '/refinance/momofukuPays',
    //   name: '百福支付',
    //   component: () => import("@/view/refinance/momofukuPays.vue")
    // },
    // //还钱结果contractSigning
    // {
    //   path: '/refinance/payBackResult',
    //   name: '还钱结果',
    //   component: () => import("@/view/refinance/payBackResult.vue")
    // },
    // //合约签订
    // {
    //   path: '/refinance/contractSigning',
    //   name: '合约签订',
    //   component: () => import("@/view/refinance/contractSigning.vue")
    // },
    
    
    // 办贷新页面
    // {
    //   path: '/loanPage/newHome',
    //   name: '个人贷款',
    //   component: () => import("@/view/loanPage/newHome.vue")
    // },
    // {
    //   path: '/loanPage/productDetail',
    //   name: '产品详情',
    //   component: () => import("@/view/loanPage/productDetail.vue")
    // },
    // {
    //   path: '/loanPage/productList',
    //   name: '产品列表',
    //   component: () => import("@/view/loanPage/productList.vue")
    // },
    // {
    //   path: '/loanPage/contractList',
    //   name: '合同列表',
    //   component: () => import("@/view/loanPage/contractList.vue")
    // },
    // {
    //   path: '/loanPage/adoptStatusPage',
    //   name: '签约状态',
    //   component: () => import("@/view/loanPage/adoptStatusPage.vue")
    // },
    // //提款预览还款计划
    // {
    //   path: '/loanPage/jkRepaymentPlan',
    //   name: '还款计划',
    //   component: () => import("@/view/loanPage/jk_repaymentPlan.vue")
    // },
    // {
    //   path: '/loanPage/allocateList',
    //   name: '我要用款',
    //   component: () => import("@/view/loanPage/allocateList.vue")
    // },
    // {
    //   path: '/loanPage/addBankCard',
    //   name: '添加银行卡',
    //   component: () => import("@/view/loanPage/addBankCard.vue")
    // },
    // {
    //   path: '/loanPage/addBankCardCode',
    //   name: '添加银行卡获取验证码',
    //   component: () => import("@/view/loanPage/addBankCardCode.vue")
    // },
    // {
    //   path: '/loanPage/repaymentHome',
    //   name: '还款',
    //   component: () => import("@/view/loanPage/repaymentHome.vue")
    // },
    // {
    //   path: '/loanPage/usageDetails',
    //   name: '用款详情',
    //   component: () => import("@/view/loanPage/usageDetails.vue")
    // },
    // {
    //   path: '/loanPage/repaymentPlanCurrent',
    //   name: '还款计划',
    //   component: () => import("@/view/loanPage/repaymentPlanCurrent.vue")
    // },
    // {
    //   path: '/loanPage/repaymentPlanAll',
    //   name: '全部还款计划',
    //   component: () => import("@/view/loanPage/repaymentPlanAll.vue")
    // },
    // {
    //   path: '/loanPage/earlyRepayment',
    //   name: '提前还款',
    //   component: () => import("@/view/loanPage/earlyRepayment.vue")
    // },
    // {
    //   path: '/loanPage/nowRepayment',
    //   name: '立即还款',
    //   component: () => import("@/view/loanPage/nowRepayment.vue")
    // },
    // {
    //   path: '/loanPage/bankEntrance',
    //   name: '银行入口',
    //   component: () => import("@/view/loanPage/bankEntrance.vue")
    // },
    // {
    //   path: '/loanPage/paymentPass',
    //   name: '支付密码',
    //   component: () => import("@/view/loanPage/paymentPass.vue")
    // },
    // {
    //   path: '/loanPage/repaymentResults',
    //   name: '还款结果',
    //   component: () => import("@/view/loanPage/repaymentResults.vue")
    // },
    // {
    //   path: '/loanPage/intentRegistration',
    //   name: '意向登记',
    //   component: () => import("@/view/loanPage/intentRegistration.vue")
    // },
    // {
    //   path: '/loanPage/IntentResults',
    //   name: '意向登记申请结果',
    //   component: () => import("@/view/loanPage/IntentResults.vue")
    // },
    // {
    //   path: '/loanPage/creditEmpowerList',
    //   name: '征信授权',
    //   component: () => import("@/view/loanPage/creditEmpowerList.vue")
    // },
    // {
    //   path: '/loanPage/applyStatusList',
    //   name: '申请进度',
    //   component: () => import("@/view/loanPage/applyStatusList.vue")
    // },
    // {
    //   path: '/loanPage/applyStatus',
    //   name: '申请进度详情页',
    //   component: () => import("@/view/loanPage/applyStatus.vue")
    // },
    // {
    //   path: '/loanPage/QRCodeForProduct',
    //   name: '二维码扫码进产品详情页',
    //   component: () => import("@/view/loanPage/QRCodeForProduct.vue")
    // },
    // {
    //   path: '/loanPage/myLoanList',
    //   name: '我的贷款',
    //   component: () => import("@/view/loanPage/myLoanList.vue")
    // }
  ]
});

function mobileBankLogin(params) {
  return new Promise((resolve, reject) => {
    Vue.baseService
      .post(Vue.__ctx + "/CIPInterface/CIP2301025", params)
      .then(res => {
        if (res.isOk) {
          resolve(res.data);
        } else {
          reject(res.msg);
        }
      })
      .catch(err => {
        reject(err);
      });
  });
}

function getQueryString(name) {
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i")
  var r = window.location.search.substr(1).match(reg)
  if (r != null) {
    return unescape(r[2])
  }
  return null;
}

const disabledRoters = process.env.VUE_APP_DISABLED_ROUTE_LIST

appRouter.beforeEach((to, form, next) => {
  // 判断当前环境路由是否禁用
  if (disabledRoters) {
    let disabledRoutes = disabledRoters.split(",")
    let routeDisable = disabledRoutes.findIndex(d => {
      return to.path.indexOf(d) != -1;
    });
    if (routeDisable != -1) {
      next("*");
      return;
    }
  }
  if (
    process.env.Equipment == "appBank" &&
    to.query.request &&
    to.query.aesKey
  ) {
    try {
      let params = {
        aesKey: getQueryString("aesKey"),
        requestParam: getQueryString("request"),
        audience: "app"
      };
      mobileBankLogin(params)
        .then(login => {
          if (login.inJson.UserUniqueId) {
            localStorage.setItem(
              "CHANNEL_INFO",
              JSON.stringify({
                appId: login.inJson.UserUniqueId
              })
            );
            next();
          } else {
            localStorage.setItem("CHANNEL_INFO", JSON.stringify({
              appId: ""
            }));
            next();
          }
        })
        .catch(err => {
          AlertModule.show({
            title: "提示信息",
            content: "获取登录信息失败，请重新进入！",
            onHide() {
              JSbridge.nativeFunc({
                method: "quit",
                nextStage: ""
              });
            }
          });
        });
    } catch (e) {
      //TODO handle the exception
      console.warn(e);
    }
  } else if (process.env.Equipment == "wxBank") {
    // UnionId=oOZBU0keJAWgyOMTet5jX0h-vjYI&ChannelId=WXB&timeStamp=1678345113%sign=6d9abba62655021d3d918324a71b1211
    if (
      to.query.UnionId &&
      to.query.ChannelId &&
      to.query.timeStamp &&
      to.query.sign
    ) {
      // if()
      let signValue =
        to.query.UnionId + "GZB" + to.query.ChannelId + to.query.timeStamp;
      let sign = md5(signValue);
      if (sign == to.query.sign) {
        localStorage.setItem(
          "CHANNEL_INFO",
          JSON.stringify({
            appId: to.query.UnionId
          })
        );
      } else {
        localStorage.setItem("CHANNEL_INFO", JSON.stringify({
          appId: ""
        }));
      }
    } else {
      // localStorage.setItem('CHANNEL_INFO',JSON.stringify({appId: ''}))
    }
    next();
  } else {
    next();
  }
});

export default appRouter;
